package com.wsoft.im.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.ImRedisConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.CommonMemberService;
import com.wsoft.core.service.RedisService;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.core.vo.MemberVO;
import com.wsoft.im.entity.ImMemberConfigEntity;
import com.wsoft.im.enums.MemberConfigEnum;
import com.wsoft.im.mapper.ImMemberConfigMapper;
import com.wsoft.im.query.member.config.MemberListQuery;
import com.wsoft.im.vo.MemberConfigVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author z001
 * @Date 2025/1/14 09:29
 */
@Service
public class ImMemberConfigManager extends ServiceImpl<ImMemberConfigMapper, ImMemberConfigEntity> {

    @Resource
    private RedisService redisService;
    @Resource
    private CommonMemberService memberService;
    /**
     * 获取类型下的 成员类别
     * @param type
     * @return
     */
    @GetRedisCache(type = ImRedisConstant.MEMBER_WHITE_LIST,
            expire_sec = RedisKeyConstant.Cms.EXPIRED,
            value = RedisKeyConstant.REDIS_NULL)
    public List<ImMemberConfigEntity> getWhiteMemberConfigList(){
        QueryWrapper<ImMemberConfigEntity> query = new QueryWrapper<>();
        query.lambda().eq(ImMemberConfigEntity::getFilterType,MemberConfigEnum.WHITE.getType());
        return list(query);
    }

    /**
     * 新增白名单
     * @param memberIdList
     * @return
     */
    public boolean addWhiteList(List<Long> memberIdList){
        if(CollUtil.isEmpty(memberIdList)){
            return false;
        }
        List<ImMemberConfigEntity> saveList = new ArrayList<>();
        for (Long memberId : memberIdList) {
            ImMemberConfigEntity entity = new ImMemberConfigEntity();
            entity.setMemberId(memberId);
            entity.setFilterType(MemberConfigEnum.WHITE.getType());
            saveList.add(entity);
        }
        boolean b = super.saveBatch(saveList);
        if(b){
            redisService.del(ImRedisConstant.MEMBER_WHITE_LIST);
        }
        return b;
    }


    /**
     * 移除白名单
     * @param memberIdList
     * @return
     */
    public boolean removeWhiteList(List<Long> memberIdList){
        if(CollUtil.isEmpty(memberIdList)){
            return false;
        }
        QueryWrapper<ImMemberConfigEntity> query = new QueryWrapper<>();
        query.lambda().in(ImMemberConfigEntity::getMemberId,memberIdList);
        boolean b = super.remove(query);
        if(b){
            redisService.del(ImRedisConstant.MEMBER_WHITE_LIST);
            for (Long memberId : memberIdList) {
                redisService.del(ImRedisConstant.MEMBER_INFO + memberId);
            }
        }
        return b;
    }

    /**
     * 根据id修改
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(ImMemberConfigEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(ImRedisConstant.MEMBER_WHITE_LIST);
            redisService.del(ImRedisConstant.MEMBER_INFO + entity.getMemberId());
        }
        return b;
    }

    /**
     * 根据id获取
     * @param memberId
     * @return
     */
    @GetRedisCache(type = ImRedisConstant.MEMBER_INFO,
            expire_sec = RedisKeyConstant.Cms.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public ImMemberConfigEntity getByMemberId(Long memberId){
        QueryWrapper<ImMemberConfigEntity> query = new QueryWrapper<>();
        query.lambda().eq(ImMemberConfigEntity::getMemberId,memberId);
        return getOne(query);
    }

    /**
     * 添加白名单/黑名单成员
     * @param addMemberId
     * @return
     */
    public boolean addWhiteBlackMember(ImMemberConfigEntity memberConfig,Long curMemberId){
        boolean b = saveOrUpdate(memberConfig);
        if(b){
            redisService.del(ImRedisConstant.MEMBER_INFO + curMemberId);
            redisService.del(ImRedisConstant.MEMBER_WHITE_LIST);
        }
        return b;
    }


    /**
     * 分页查询
     * @param param
     * @return
     */
    public List<MemberConfigVO> listMember(MemberListQuery param) {
        QueryWrapper<ImMemberConfigEntity> query = new QueryWrapper<>();
        List<MemberVO> memberList = memberService.getMemberList();

        if(StrUtil.isNotBlank(param.getMemberName())){
            List<Long> memberIdList = memberList.stream().filter(item -> item.getName().contains(param.getMemberName())).map(MemberVO::getId).collect(Collectors.toList());
            query.lambda().in(ImMemberConfigEntity::getMemberId,memberIdList);
        }
        query.lambda().eq(ImMemberConfigEntity::getMemberId, LoginUserUtil.getUserInfo().getLoginUserVO().getId());
        //查询用户配置信息
        List<ImMemberConfigEntity> list = list(query);
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        ImMemberConfigEntity configEntity = list.get(0);
        List<MemberVO> memberVOList = new ArrayList<>();
        //用户白名单/黑名单人员id列表
        List<String> values = new ArrayList<>();
        if(Objects.equals(configEntity.getFilterType(), MemberConfigEnum.WHITE.getType())){
            values = StrUtil.split(configEntity.getWhiteList(), StrUtil.COMMA);
        }else{
            values = StrUtil.split(configEntity.getBlackList(), StrUtil.COMMA);
        }
        if(CollUtil.isEmpty(values)){
            return new ArrayList<>();
        }
        List<String> finalValues = values;
        //转换返回类
        memberVOList = memberList.stream().filter(item -> finalValues.contains(item.getId().toString())).collect(Collectors.toList());
        return memberVOList.stream().map(item-> Convert.convert(MemberConfigVO.class,item)).collect(Collectors.toList());
    }

}
